package easytalk.dialogs;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

import connection.ServerConnectionConfig;
import connection.ServerType;
import easytalk.media.EasyTalkImages;
import easytalk.views.custom.ConnectionProgress;

public class AuthDialog extends TitleAreaDialog {

	private Text userNameText;
	private Text passwordText;
	private ServerConnectionConfig config = new ServerConnectionConfig();
	private Combo combo;
	private boolean saveConnection = false;

	public AuthDialog(Shell parentShell) {
		super(parentShell);
	//	setShellStyle(SWT.APPLICATION_MODAL | SWT.Resize | SWT.CLOSE);
	}

	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		// Set the title
		setTitle(Messages.getString("AuthDialog.0")); //$NON-NLS-1$
		// Set the message
		setMessage(Messages.getString("AuthDialog.1"), //$NON-NLS-1$
				IMessageProvider.INFORMATION);
		setTitleImage(EasyTalkImages.DEFAULT_SMILEY);
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		// return super.createDialogArea(parent);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.verticalSpacing = 5;
		layout.makeColumnsEqualWidth = true;
		parent.setLayout(layout);

		Label serverlabel = new Label(parent, SWT.NONE);
		serverlabel.setText(Messages.getString("AuthDialog.2")); //$NON-NLS-1$
		serverlabel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

		combo = new Combo(parent, SWT.READ_ONLY);
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		String[] list = new String[ServerType.values().length];
		int i = 0;
		for (ServerType type : ServerType.values()) {
			list[i++] = type.name();
			combo.setData(list[i - 1], type);
		}
		combo.setItems(list);
		combo.select(i - 1);
		config.setServerType((ServerType) combo.getData(combo.getText()));
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				config.setServerType((ServerType) combo
						.getData(combo.getText()));

				updateButtons();

				super.widgetSelected(e);
			}
		});

		Label label1 = new Label(parent, SWT.NONE);
		label1.setText(Messages.getString("AuthDialog.3")); //$NON-NLS-1$
		label1.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
		userNameText = new Text(parent, SWT.BORDER);
		userNameText.setText(Messages.getString("AuthDialog.4")); //$NON-NLS-1$
		userNameText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
		
		
		Label label2 = new Label(parent, SWT.NONE);
		label2.setText(Messages.getString("AuthDialog.5")); //$NON-NLS-1$
		label2.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
		
		passwordText = new Text(parent, SWT.BORDER | SWT.PASSWORD);
		passwordText.setText(Messages.getString("AuthDialog.6")); //$NON-NLS-1$
		passwordText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

		Button saveConnectionButton = new org.eclipse.swt.widgets.Button(
				parent, SWT.CHECK);
		saveConnectionButton.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,
				false));
		saveConnectionButton.setText(Messages.getString("AuthDialog.7")); //$NON-NLS-1$

		saveConnectionButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				saveConnection = ! saveConnection;
			}
		});
		/*
		 * RadioGroupFieldEditor radioGroup = new RadioGroupFieldEditor(
		 * "Vue","vue",1,new );
		 */

		return parent;
	}

	public ServerConnectionConfig getConfig() {
		return config;
	}

	public String getPassword() {
		return config.getPassword();
	}

	public ServerType getType() {
		return config.getServerType();
	}

	public String getUserName() {
		return config.getUserName();
	}

	public boolean isSaveConnection() {
		return saveConnection;
	}

	@Override
	protected void okPressed() {

		this.config.setUserName(userNameText.getText());
		this.config.setPassword(passwordText.getText());
		ConnectionProgress progress = new ConnectionProgress(getConfig());
		progress.init(PlatformUI.getWorkbench().getActiveWorkbenchWindow());
		progress.run(null);
		if (!progress.isError()) {
			if (saveConnection) {
				this.config.save();
			}
			super.okPressed();
		}
		else {
			this.setErrorMessage(Messages.getString("AuthDialog.8") + progress.getErrorMessage()); //$NON-NLS-1$
		}

	}

	public void setConfig(ServerConnectionConfig config) {
		this.config = config;
	}

	public void setSaveConnection(boolean saveConnection) {
		this.saveConnection = saveConnection;
	}

	private void updateButtons() {
		switch (config.getServerType()) {
		case SKYPE:
		case OFFILNE:
			userNameText.setEnabled(false);
			passwordText.setEnabled(false);
			break;
		case MSN:
		case GOOGLE_TALK:
			userNameText.setEnabled(true);
			passwordText.setEnabled(true);
			break;
		default:
			break;
		}

	}

}
